package cn.onesdream.app.api.controller;

import cn.onesdream.app.api.service.PlanetService;
import cn.onesdream.app.common.util.ResponseResult;
import cn.onesdream.app.dao.entity.Planet;
import cn.onesdream.app.dao.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author WangShiyu
 * @date 2021/2/9 22:59
 */
@RestController
@RequestMapping("/api/planet")
@Slf4j
@Api(tags = "星球相关接口")
@Validated
public class PlanetController {
    @Autowired
    private PlanetService planetService;

    @ApiOperation("通过userId来获取公开星球")
    @GetMapping("/getPublicByUserId/{userId}")
    public ResponseResult<List> getPublicByUserId(@PathVariable("userId") @NotNull Integer userId){
        List<Planet> planetList = planetService.getPublicPlanet(userId);
        return new ResponseResult<>(planetList);
    }

    @ApiOperation("获取自己所有星球")
    @GetMapping("/getAllPlanet")
    public ResponseResult<List> getAllPlanet(){
        Integer userId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        List<Planet> planetList = planetService.getPlanetByUserId(userId);
        return new ResponseResult<>(planetList);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "count", value = "随机获取的星球信息数量")
    })
    @ApiOperation("随机获取星球")
    @GetMapping("/getRandomPlanet")
    public ResponseResult<List> getRandomPlanet(Byte count){
        List<Planet> planetList = planetService.getRandomPlanet(count);
        return new ResponseResult<>(planetList);
    }

    @ApiOperation("删除星球")
    @PostMapping("/deletePlanet")
    public ResponseResult<Planet> deletePlanet(Integer planetId){
        planetService.deletePlanet(planetId);
        return new ResponseResult<>();
    }

    @ApiOperation("通过星球id获取星球")
    @GetMapping("/{planetId}")
    public ResponseResult<Planet> planet(@PathVariable("planetId") @NotNull Integer planetId){
        Planet planet = planetService.getPlanet(planetId);
        return new ResponseResult<>(planet);
    }

    @ApiOperation("修改星球信息")
    @PostMapping("/changePlanetInfo")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "privateId", value = "私密状态，0：公开，1：私人，不传时默认为0")
    })
    public ResponseResult<Planet> changePlanetInfo(
            Integer planetId,
            String name,
            String description,
            String shareUserId,
            Byte privateId){
        Planet planet = planetService.changePlanetInfo(planetId, name, description, shareUserId, privateId);
        return new ResponseResult<>(planet);
    }

    @ApiOperation("通过shareUserId来获取星球")
    @GetMapping("/getByShareUserId/{shareUserId}")
    public ResponseResult<List> getByShareUserId(@PathVariable("shareUserId") @NotNull Integer shareUserId){
        List<Planet> planetList = planetService.getPlanetByShareUserId(shareUserId);
        return new ResponseResult<>(planetList);
    }

    @ApiOperation("通过ownUserId来获取星球")
    @GetMapping("/getByOwnUserId/{ownUserId}")
    public ResponseResult<List> getByOwnUserId(@PathVariable("ownUserId") @NotNull Integer ownUserId){
        List<Planet> planetList = planetService.getPlanetByOwnUserId(ownUserId);
        return new ResponseResult<>(planetList);
    }

    @ApiOperation("创建星球")
    @PostMapping("/")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "description", value = "可不传"),
            @ApiImplicitParam(name = "shareUserId", value = "json数据类型以[]格式的字符串上传"),
            @ApiImplicitParam(name = "typeId", value = "星球类型，1：记录星球，2：分享星球，3：认知星球"),
            @ApiImplicitParam(name = "privateId", value = "私密状态，0：公开，1：私人，不传时默认为0")
    })
    public ResponseResult<Planet> postPlanet(String name, String description, String shareUserId, Byte typeId, Byte privateId)throws IOException {
        Integer ownUserId = ((User) SecurityContextHolder.getContextHolderStrategy().getContext().getAuthentication().getPrincipal()).getId();
        planetService.addPlanet(name, description, ownUserId, shareUserId, typeId, privateId);
        return new ResponseResult<>();
    }
}
